//
//  CreatePlotTableViewController.m
//  PlotMate
//
//  Created by Brian Boyle on 04/04/2012.
//  Copyright (c) 2012 Brian Boyle. All rights reserved.
//

#import "CreatePlotTableViewController.h"
#import "SoilAndLightTypeViewController.h"
#import "TempPlot+Create.h"
#import "AppDelegate.h"

@interface CreatePlotTableViewController()
@property (nonatomic, strong) UITapGestureRecognizer *tap;
@property (assign) BOOL soilTypeTapped;
@property (assign) BOOL lightConditionsTapped;
@property (nonatomic, strong) TempPlot *tempPlot;
@end


@implementation CreatePlotTableViewController

@synthesize stepper = _stepper;
@synthesize numberOfBedsLabel = _numberOfBedsLabel;
@synthesize nameField = _nameField;
@synthesize soilTypeCell = _soilTypeCell;
@synthesize lightConditionsCell = _lightConditionsCell;
@synthesize tap = _tap;
@synthesize soilTypeTapped = _soilTypeTapped;
@synthesize lightConditionsTapped = _lightConditionsTapped;
@synthesize tempPlot = _tempPlot;

-(void)configureStepper
{
    self.stepper.minimumValue = 1;
    self.stepper.maximumValue = 1000;
    self.stepper.stepValue = 1;
    self.stepper.wraps = NO;
    self.stepper.autorepeat = YES;
    self.stepper.continuous = YES;
}

#pragma mark - View lifecycle

-(void)viewDidLoad
{
    [self configureStepper];
    self.numberOfBedsLabel.text = [NSString stringWithFormat:@"%.f", self.stepper.value];
    
    //Set the background image
    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"paper.png"]];
    [tempImageView setFrame:self.tableView.frame]; 
    self.tableView.backgroundView = tempImageView;
    
    self.navigationItem.rightBarButtonItem.enabled = NO;
    //Add an editing change event to the namefield to perform for validation
    [self.nameField addTarget:self action:@selector(performValidation) forControlEvents:UIControlEventEditingChanged];
}

- (void)viewDidUnload
{
    [self setNumberOfBedsLabel:nil];
    [self setStepper:nil];
    [self setNameField:nil];
    [self setSoilTypeCell:nil];
    [self setLightConditionsCell:nil];
    [super viewDidUnload];
}

-(void)setFonts
{
    UITableViewCell *bedNumberCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    bedNumberCell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
    bedNumberCell.backgroundColor = [UIColor clearColor];
    
    UILabel *numberBedsText = (UILabel *)[bedNumberCell viewWithTag:1];
    numberBedsText.font = [UIFont fontWithName:@"MV Boli" size:16];
    numberBedsText.textColor = [UIColor colorWithRed:0 green:0.275 blue:0.118 alpha:1];
    
    UILabel *numberBeds = (UILabel *)[bedNumberCell viewWithTag:2];
    numberBeds.font = [UIFont fontWithName:@"Sketch Rockwell" size:32];
    numberBeds.textColor = [UIColor colorWithRed:0 green:0.275 blue:0.118 alpha:1];
    
    //Soil Cell
    UITableViewCell *soilCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]];
    soilCell.textLabel.font = [UIFont fontWithName:@"Sketch Rockwell" size:16];
    soilCell.textLabel.textColor = [UIColor colorWithRed:0 green:0.275 blue:0.118 alpha:1];
    soilCell.detailTextLabel.textColor = [UIColor colorWithRed:0 green:0.275 blue:0.118 alpha:1];
    
    //Light Cell
    UITableViewCell *lightCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:3]];
    lightCell.textLabel.font = [UIFont fontWithName:@"Sketch Rockwell" size:16];
    lightCell.textLabel.textColor = [UIColor colorWithRed:0 green:0.275 blue:0.118 alpha:1];
    lightCell.detailTextLabel.textColor = [UIColor colorWithRed:0 green:0.275 blue:0.118 alpha:1];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UITableViewCell *nameCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    nameCell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
    nameCell.backgroundColor = [UIColor clearColor];
    
    //Set the class as the textField's delegate
    self.nameField.delegate = self;
    self.nameField.borderStyle = UITextBorderStyleRoundedRect;
    
    [self setFonts];
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"selectSoilTypeSegue"]){
        self.soilTypeTapped = YES;
        SoilAndLightTypeViewController *soilLightVC = segue.destinationViewController;
        soilLightVC.headingText = @"Select a general Soil Type for your plot";
        [soilLightVC setPickerArray:[[NSArray alloc] initWithObjects:@"Rich, deep soil, firm with plenty of well rotted compost dug in",
                                                         @"Fertile well-drained soil", @"Light, deep, well-drained soil", @"Rich soil with plenty of compost added", nil]];
        
        [segue.destinationViewController setDelegate:self];
    }else if([segue.identifier isEqualToString:@"selectLightConditionsSegue"]){
        self.lightConditionsTapped = YES;
        SoilAndLightTypeViewController *soilLightVC = segue.destinationViewController;
        soilLightVC.headingText = @"Select the general Light Coniditions for your plot";
        [segue.destinationViewController setPickerArray:[[NSArray alloc] initWithObjects:@"Full Sun", @"Sun or Partial Shade", @"Shade or semi-shade", nil]];
        [segue.destinationViewController setDelegate:self];
    }
    else if([segue.identifier isEqualToString:@"addBedSegue"]){
        self.tempPlot = [TempPlot createTempPlotWithName:self.nameField.text 
                                                    soil:self.soilTypeCell.detailTextLabel.text 
                                                   light:self.lightConditionsCell.detailTextLabel.text 
                                            numberOfBeds:[self.numberOfBedsLabel.text intValue]];

        [segue.destinationViewController setTempPlot:self.tempPlot];
    }
    
}

- (IBAction)cancelButtonPressed:(UIBarButtonItem *)sender 
{
    [self.presentingViewController dismissModalViewControllerAnimated:YES];
}

/*Validate the nameField to see if data is entered into it. If it has data then enable the Next button*/
-(void)performValidation
{
    if(self.nameField.text.length > 0 && self.soilTypeCell.detailTextLabel.text.length > 0 
       && self.lightConditionsCell.detailTextLabel.text.length > 0){
        self.navigationItem.rightBarButtonItem.enabled = YES;
    }else{
        self.navigationItem.rightBarButtonItem.enabled = NO;
    }
  
}

- (IBAction)bedStepperChangedValue:(UIStepper *)sender 
{
    double stepperValue = sender.value;
    if(stepperValue > 0){
        self.numberOfBedsLabel.text = [NSString stringWithFormat:@"%.f", stepperValue];
    }
}

- (IBAction)nameFieldTouched:(UITextField *)sender 
{
    self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:self.tap];
}

-(BOOL)textFieldShouldClear:(UITextField *)textField
{
    return YES;
}


-(void)dismissKeyboard {
    [self.nameField resignFirstResponder];
    [self.view removeGestureRecognizer:self.tap];
}

#pragma mark - SoilOrLightTypeProtocol methods

/*Implementation of delegate method for handling the selection of a light conidition or soil type*/
-(void)soilOrLightSelectedWithValue:(NSString *)value
{
    if(self.soilTypeTapped == YES){
        self.soilTypeCell.detailTextLabel.text = value;
        self.soilTypeTapped = NO;
        
    }else if(self.lightConditionsTapped == YES){
        self.lightConditionsCell.detailTextLabel.text = value;
        self.lightConditionsTapped = NO;
    }
    [self performValidation];
}

@end
